﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using JiveMessenger.Network;
using JmShared;
using System.Collections.ObjectModel;
using JiveMessenger.Utilities;


namespace JiveMessenger.Models
{
    public class JiveContactModel : INotifyPropertyChanged, IEquatable<JiveContactModel>, IContact
    {
        public static readonly JiveContactModel GreenText = new JiveContactModel("1", "GreenText@Jive.com", "GreenText", DeviceType.PC) { FontColor = new SolidColorBrush(Colors.Green), FontStyle = FontStyles.Normal, FontFamily = StaticMethods.ConvertFromString<FontFamily>("Segoe UI"), FontWeight = FontWeights.Normal };
        public static readonly JiveContactModel RedText = new JiveContactModel("2", "RedText@Jive.com", "RedText", DeviceType.PC) { FontColor = new SolidColorBrush(Colors.Maroon), FontStyle = FontStyles.Normal, FontFamily = StaticMethods.ConvertFromString<FontFamily>("Segoe UI"), FontWeight = FontWeights.Normal };
        public static readonly JiveContactModel GrayText = new JiveContactModel("3", "GrayText@Jive.com", "GrayText", DeviceType.PC) { FontColor = new SolidColorBrush(Colors.DimGray), FontStyle = FontStyles.Normal, FontFamily = StaticMethods.ConvertFromString<FontFamily>("Segoe UI"), FontWeight = FontWeights.Normal };



        public string UserName { get; set; }
        public string UserID
        {
            get
            {
                return this.UserName + "#" + this.DeviceID;
            }
        }
        public string DeviceID { get; set; }

        public object Instance
        {
            get { return this; }
        }
        /// <summary>
        /// Returns the Nickname if available, else the Username
        /// </summary>
        public string PrettyName
        {
            get
            {
                return !String.IsNullOrEmpty(this.NickName) ? this.NickName : this.UserName;
            }
        }

        private string _nickName;
        public string NickName
        {
            get { return _nickName; }
            set
            {
                if (value != this._nickName)
                {
                    this._nickName = value;
                    RaisePropertyChanged("NickName");
                }
            }
        }

        private ClientStatus _clientStatus;
        public ClientStatus ClientStatus
        {
            get { return _clientStatus; }
            set
            {
                if (_clientStatus != value)
                {
                    _clientStatus = value;
                    RaisePropertyChanged("ClientStatus");
                }
            }
        }
        private DeviceType _deviceType;
        public DeviceType DeviceType
        {
            get { return _deviceType; }
            set
            {
                if (_deviceType != value)
                {
                    _deviceType = value;
                    RaisePropertyChanged("DeviceType");
                }
            }
        }


        private DateTime _afkStart;
        public DateTime AfkStart
        {
            get { return _afkStart; }
            set
            {
                if (_afkStart != value)
                {
                    _afkStart = value;
                    RaisePropertyChanged("AfkStart");
                }
            }
        }

        private DateTime _offlineStart;
        public DateTime OfflineStart
        {
            get { return _offlineStart; }
            set
            {
                if (_offlineStart != value)
                {
                    _offlineStart = value;
                    RaisePropertyChanged("OfflineStart");
                }
            }
        }




        private int _unreadMessageCount;
        public int UnreadMessageCount
        {
            get { return _unreadMessageCount; }
            set
            {
                if (value != this._unreadMessageCount)
                {
                    this._unreadMessageCount = value;
                    RaisePropertyChanged("UnreadMessageCount");
                }
            }
        }


        private string _pmStatus;
        public string PmStatus
        {
            get { return _pmStatus; }
            set
            {
                if (value != this._pmStatus)
                {
                    this._pmStatus = value;
                    RaisePropertyChanged("PmStatus");
                }
            }
        }

        public string ToolTipText
        {
            get
            {
                string text = this.UserName;
                if (this.ClientStatus == JmShared.ClientStatus.Afk && this.AfkStart != DateTime.MinValue)
                    text += string.Format(" - AFK for {0}", ToFormattedTimeString(DateTime.Now.Subtract(this.AfkStart)));
                else if(this.ClientStatus == JmShared.ClientStatus.Offline && this.OfflineStart != DateTime.MinValue)
                    text += string.Format(" - Offline for {0}", ToFormattedTimeString(DateTime.Now.Subtract(this.OfflineStart)));
                return text;
            }
        }

        private ObservableCollection<IChatGroup> _chatGroups;
        public ObservableCollection<IChatGroup> ChatGroups
        {
            get { return _chatGroups; }
            set
            {
                if (_chatGroups != value)
                {
                    _chatGroups = value;
                    RaisePropertyChanged("ChatGroups");
                }
            }
        }

        private ObservableCollection<JiveCustomEmoticonModel> _customEmoticons;
        public ObservableCollection<JiveCustomEmoticonModel> CustomEmoticons
        {
            get { return _customEmoticons; }
            set
            {
                if (_customEmoticons != value)
                {
                    _customEmoticons = value;
                    RaisePropertyChanged("CustomEmoticons");
                }
            }
        }



        private bool _isIgnored;
        public bool IsIgnored
        {
            get { return _isIgnored; }
            set
            {
                if (_isIgnored != value)
                {
                    _isIgnored = value;
                    RaisePropertyChanged("IsIgnored");
                }
            }
        }


        private ImageSource _avatar;
        public ImageSource Avatar
        {
            get { return _avatar; }
            set
            {
                if (_avatar != value)
                {
                    _avatar = value;
                    RaisePropertyChanged("Avatar");
                }
            }
        }



        private FontFamily _fontFamily;
        public FontFamily FontFamily
        {
            get
            {
                if (_fontFamily == null)
                {
                    _fontFamily = StaticMethods.ConvertFromString<FontFamily>("Segoe UI");
                }
                return _fontFamily;
            }
            set
            {
                if (_fontFamily != value)
                {
                    _fontFamily = value;
                    RaisePropertyChanged("FontFamily");
                }
            }
        }

        private FontWeight _fontWeight;
        public FontWeight FontWeight
        {
            get { return _fontWeight; }
            set
            {
                if (_fontWeight != value)
                {
                    _fontWeight = value;
                    RaisePropertyChanged("FontWeight");
                }
            }
        }

        private FontStyle _fontStyle;
        public FontStyle FontStyle
        {
            get { return _fontStyle; }
            set
            {
                if (_fontStyle != value)
                {
                    _fontStyle = value;
                    RaisePropertyChanged("FontStyle");
                }
            }
        }

        private double _fontSize;
        public double FontSize
        {
            get { return _fontSize; }
            set
            {
                if (_fontSize != value)
                {
                    _fontSize = value;
                    RaisePropertyChanged("FontSize");
                }
            }
        }

        private SolidColorBrush _fontColor;
        public SolidColorBrush FontColor
        {
            get { return _fontColor; }
            set
            {
                if (_fontColor != value)
                {
                    _fontColor = value;
                    RaisePropertyChanged("FontColor");
                }
            }
        }

        private bool _isFriend;
        public bool IsFriend
        {
            get { return _isFriend; }
            set
            {
                if (_isFriend != value)
                {
                    _isFriend = value;
                    RaisePropertyChanged("IsGroupContact");
                }
            }
        }

        private bool _showInContactList;
        public bool ShowInContactList
        {
            get { return _showInContactList; }
            set
            {
                if (_showInContactList != value)
                {
                    _showInContactList = value;
                    RaisePropertyChanged("ShowInContactList");
                }
            }
        }



        public JiveContactModel(string deviceID, string username, string nickName, DeviceType deviceType, bool isFriend = true)
        {
            this.DeviceID = deviceID;
            this.UserName = username;
            this.NickName = nickName;
            this.ClientStatus = JmShared.ClientStatus.Available;
            this.ChatGroups = new ObservableCollection<IChatGroup>();
            this.IsFriend = isFriend;
        }
        public JiveContactModel()
        {
        }
        private string ToFormattedTimeString(TimeSpan span)
        {
            string answer;
            if (span.TotalMinutes < 1.0)
            {
                answer = String.Format("{0}s", span.Seconds);
            }
            else if (span.TotalHours < 1.0)
            {
                answer = String.Format("{0}m{1:D2}s", span.Minutes, span.Seconds);
            }
            else if(span.TotalDays<1) // more than 1 hour,less then one day
            {
                answer = String.Format("{0}h{1:D2}m", (int)span.Hours, span.Minutes);
            }
            else //one day or more
            {
                answer = String.Format("{0} days", (int)span.TotalDays);
            }

            return answer;
        }
        public override int GetHashCode()
        {
            return this.UserID.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as JiveContactModel);
        }
        public bool Equals(JiveContactModel other)
        {
            if (other == null)
                return false;
            return this.UserID == other.UserID;
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;


    }

}
